package cn.edu.thu.tsqualitylite.controller.util;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Controller
public class FilesUploadController {

  @RequestMapping(value = "fileUpload", method = RequestMethod.POST)
  @CrossOrigin
  public String fileUpload(@RequestParam("fileName") MultipartFile file) {
    if (file.isEmpty()) {
      return "false";
    }
    String fileName = file.getOriginalFilename();
    int size = (int) file.getSize();

    String path = "//Users/myn-thss/Documents/TsQualityFile/data";
    File dest = new File(path + "/" + fileName);
    if (!dest.getParentFile().exists()) { //判断文件父目录是否存在
      dest.getParentFile().mkdir();
    }
    try {
      file.transferTo(dest); //保存文件
      return "true";
    } catch (IllegalStateException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      return "false";
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      return "false";
    }
  }

}
